/**=========================================================
 * Module: utils.js
 * create by LDM
 * 2018-06-21
 =========================================================*/


(function ($, window) {
  'use strict';

  var $win = $(window);
  $.Utils = $.Utils || {};
  var isPublish = false,  //备注:部署服务器的时候,设置成true- //
    targetIpPort = 'api.huishiguang.com',
    debugServer = 'http://' + targetIpPort;

  $.Utils.urlPath = function (uri, useLocal) {
    if (!uri&&uri!="") return;
    var server;
    server = isPublish ? "" : useLocal ? "/mock" : debugServer;
    return uri.indexOf('/') == 0 ? (server + uri) : (server + '/' + uri);
  };

  $.Utils.wsPath = function (path) {
    return isPublish ? ('ws://' + location.hostname + ((location.port) ? (':' + location.port) : '') + path) :
      ('ws://' + targetIpPort + path);
  };
  $.Utils.bigDataWs = null;

  // 关闭日志
  $.Utils._orgConsoleLog = console.log;
  isPublish ? (console.log = function () {
  }) : '';


  let winWidth = document.documentElement.clientWidth;
  let winHeight = document.documentElement.clientHeight;
  /**
   * 监听窗口大小改变
   */
  $.Utils.resize = function () {
    console.log("窗口大小改变监听");
    let newWinW = document.documentElement.clientWidth;
    let newWinH = document.documentElement.clientHeight;
    if (Math.abs(winWidth - newWinW) > 50 || Math.abs(winHeight != newWinH) > 50)
      window.top.location.reload();
  };
  /**
   * 飘字
   * @param textContent
   * @param secondNum
   * @param cb
   */
  $.Utils.showBarrage = function (textContent, secondNum, cb) {
    if (textContent == null || textContent == '') {
      return;
    }
    let barrageHtml = "";
    barrageHtml += '<div class="barrage" style=" border-radius: 10px;position: fixed; top:50%; left:50%; padding: 0 50px; height:50px;margin-top: -25px;line-height: 50px;text-align: center;color: #FFFFFF;background: rgba(0, 0, 0, 0.4);z-index: 99999;">';
    barrageHtml += '<span class="barrage_text">' + textContent + '</span>';
    barrageHtml += '</div>';
    $("body").append(barrageHtml);
    let barrageWidth = $(".barrage").width();
    barrageWidth = (-1 * barrageWidth / 2) - 50;
    $(".barrage").css("margin-left", barrageWidth);
    $(".barrage").css("margin-top", -50);
    $('.barrage').click(function () {
      $(this).remove();
    });
    let second = secondNum || 2000;
    $(".barrage").find(".barrage_text").text(textContent);
    setTimeout(function () {
      if ($(".barrage").length > 0) {
        $('.barrage').remove();
      }
      cb && cb();
    }, second)
  };

  /**
   * 判断用户是否退出登录
   * @param data
   * @param cb
   */
  $.Utils.checkError = function (data, cb) {
    if (data.code == "err_not_auth" || data.code == "err_not_login") {
      $.Utils.showBarrage(data.msg || "请重新登录", 1000, function () {
        window.top.location.href = "../../index.html";
      });
    }else if (data.code != 'success' && data.msg) {
      $.Utils.showBarrage(data.msg);
    }
    cb&&cb();
  };
  /**
   * 判断是不是ie10
   * @returns {boolean}
   */
  $.Utils.isIE10 = function () {
    let navi = window.navigator.userAgent.toLowerCase();
    return navi.indexOf("msie 10") != -1;
  };

  Date.prototype.format = function (format) {
    let date = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor((this.getMonth() + 3) / 3),
      "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (let k in date) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
      }
    }
    return format;
  };
  /**
   * 对时间戳转换成时间格式
   * @param data
   * @param style
   * @returns {*}
   */
  $.Utils.setDateFormat = function (data, style) {
    let date = new Date(data);
    style = style ? style : 'yyyy-MM-dd';
    return date.format(style);
  }; /**
   *
   * @param data 需要四舍五入的值
   * @param n 保留几位小数
   * @param isOk 是否四舍五入
   * @returns {number}
   */
  $.Utils.toFixedFunc = function(data,n,isOk) {
    let oldData = data;
    let x = 10;
    for(let i=1;i<n;i++){
      x = 10*x;
    }
    data = parseInt(oldData*x);
    let newData = parseInt(oldData*x*10);
    let y = parseInt(newData-data*10)>=5?1:0;
    data = isOk?parseInt(data+y):parseInt(data);
    return data/x;
  };


}(jQuery, window));
